
import UIKit

class XWServerTableViewCell: UITableViewCell,UITableViewDataSource,UITableViewDelegate{
    
    
    @IBOutlet weak var XWTitleLabel: UILabel!
    
    @IBOutlet weak var XWTableView: UITableView!
    override func awakeFromNib() {
        super.awakeFromNib()
        self.contentView.backgroundColor = kClearColor
        self.selectionStyle = .none
        XWTableView.separatorStyle = .none
        XWTableView.separatorColor = kClearColor
        XWTableView.rowHeight = 90
        XWTableView.backgroundColor = kClearColor
        XWTableView.showsVerticalScrollIndicator = false
        XWTableView.isScrollEnabled = false
        XWTableView.register(UINib.init(nibName: "XWChildServerTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
        XWTableView.delegate = self
        XWTableView.dataSource = self
    }
    var model:XWServerModel?{
        didSet{
            XWTableView.reloadData()
        }
    }
    
    var indexCellRow:Int? {
        didSet{
            switch indexCellRow {
            case 0:
                XWTitleLabel.text = "Priority"
                break
            case 1:
                XWTitleLabel.text = "Inferior"
                break
            default:
                break
            }
        }
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if indexCellRow == 0 {
            return model == nil ? 0 : 1
        }else{
            return model == nil ? 0 : (model?.nodes!.count)!
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! XWChildServerTableViewCell
        cell.indexCellRow = indexCellRow
        if indexCellRow == 0{
            cell.childModel = model?.best_node
        }else{
            cell.childModel = model?.nodes![indexPath.row]
        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexCellRow == 0 {
            if !(model?.best_node?.isConnectVPN)!{
                XWLinkServer((model?.best_node)!)
            }
        }else {
            let tempModel = model?.nodes![indexPath.row]
            if !(tempModel?.isConnectVPN)! {
                XWLinkServer(tempModel!)
            }
        }
    }
}
extension XWServerTableViewCell {
    private func XWLinkServer(_ childModel:XWServerListModel) {
        let linkServerView = XWLinkServerView.init(frame: CGRect.zero)
        linkServerView.XWTableView = (currentViewController() as! XWServerViewController).XWTableView
        linkServerView.model = model
        linkServerView.childModel = childModel
        linkServerView.indexCellRow = indexCellRow
        currentViewController().view.addSubview(linkServerView)
        linkServerView.snp.makeConstraints { make in
            make.top.equalTo(currentViewController().view)
            make.left.equalTo(currentViewController().view)
            make.bottom.equalTo(currentViewController().view)
            make.right.equalTo(currentViewController().view)
        }
    }
}
